package org.paul.library.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import org.paul.library.base.helper.ActListener;
import org.paul.library.manager.ThreadManager;
import org.paul.library.utils.Logger;

public abstract class BaseAct extends AppCompatActivity implements ActListener {

    private java.util.logging.Logger logger = Logger.getLogger(getClass().getName());

    private ProgressDialog progressDialog;
    private int requestNum;
    protected ThreadManager threadManager = ThreadManager.getInstance();
    protected final int SHOW_PROGRESS = 0x99;
    protected final int DISMISS_PROGRESS = 0x9a;

    protected Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            dismissProgress();
            return false;
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        logger.setLevel(Level.INFO);
        App.getInstance().addAct(this);
        setContentView(getLayoutId());
        initUi();
        logger.info("act has been created.");
    }

    protected abstract void initUi();

    protected abstract int getLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getInstance().removeAct(this);
    }

    protected final <V extends View> V $(@IdRes int resId) {
        return (V) findViewById(resId);
    }

    protected final <V extends View> V $(View parent, @IdRes int resId) {
        return (V) (parent.findViewById(resId));
    }

    protected final void startAct(Class clz) {
        startWithData(clz, null);
    }

    protected final void startWithData(Class clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

//    protected class HandleRetImplFactory {
//        /**
//         * 生产观察者 当网络数据返回时候回馈给调用者
//         *
//         * @param handleRet
//         * @param <T>
//         * @return
//         */
//        public <T extends BaseBean> ThreadManager.HandleRetImpl<T> createImpl(ThreadManager.HandleRet<T> handleRet) {
//
//            return new ThreadManager.HandleRetImpl<T>() {
//                @Override
//                public void handleRet(T t) {
//                    handleRet.handleRet(t);
//                }
//            };
//        }
//    }

    protected View.OnClickListener backPressedListener = (v) -> onBackPressed();

//    protected HandleRetImplFactory handleRetImplFactory = new HandleRetImplFactory();

    protected void handle(BaseBean bean, int success, int failed) {
        if (canHandle(bean)) {
            getHandler().obtainMessage(success, bean).sendToTarget();
        } else {
            getHandler().sendEmptyMessage(failed);
        }
    }

    protected abstract Handler getHandler();

    protected Handler handler;

    @Override
    public boolean showProgress() {
        requestNum++;
        if (null == progressDialog) {
            initProgress();
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
            return true;
        }
        return false;
    }

    @Override
    public boolean dismissProgress() {
        if (requestNum == 0) {
            return false;
        }
        requestNum--;
        if (requestNum == 0) {
            if (null != progressDialog && progressDialog.isShowing()) {
                progressDialog.dismiss();
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean showNoData() {
        return false;
    }

    @Override
    public boolean dismissNoData() {
        return false;
    }

    @Override
    public boolean showNoNet() {
        return false;
    }

    @Override
    public boolean dismissNoNet() {
        return false;
    }

    @Override
    public boolean showNormal() {
        return false;
    }

    @Override
    public boolean dismissNormal() {
        return false;
    }

    @Override
    public boolean reload() {
        return false;
    }

    @Override
    public boolean canHandle(BaseBean bean) {
        return null != bean;
    }

    private void initProgress() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
        progressDialog.setCancelable(true);// 设置是否可以通过点击Back键取消
        progressDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
        // 设置提示的title的图标，默认是没有的，如果没有设置title的话只设置Icon是不会显示图标的
        progressDialog.setTitle(getLoadingTip());
        progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                requestNum = 0;
            }
        });
    }

    protected CharSequence getLoadingTip() {
        return "Loading..";
    }

}
